<?php

class FichesAction extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('ActionsDeCommunication/ActionDeCommunication');
		$this->load->model('ActionsDeCommunication/FicheAction');
		$this->load->model('ActionsDeCommunication/Tache');
		$this->load->model('ActionsDeCommunication/Depense');
		$this->load->model('ActionsDeCommunication/Recette');
		$this->load->model('Utilisateurs/Participe');
		$this->load->model('Utilisateurs/Etudiants');
		include model_chemin('putDateJJMMAAAA');
	}
	
	public function formulaire($action, $idAction)
	{
		$data = array();
		$data['onglet_ouvert'] = 'actiondecommunication';
		$data['action']= $action;
		$data['idAction']=$idAction;
		
		$fba = new FicheAction();
		
		if( $action == 'modifier')
		{
			
			$tache=new Tache();
			$depense=new Depense();
			$recette=new Recette();
			
			$fba=FicheAction::findByIdAction($idAction);
			$tache=Tache::findByIdAction($idAction);
			$depense=Depense::findByIdAction($idAction);
			$recette=Recette::findByIdAction($idAction);
			$data['fba']=$fba;
			$data['depense']=$depense;
			$data['recette']=$recette;
			$data['tache']=$tache;
			
			$data['action']= 'creer';
		}
			
		$etuParticipant = ActionDeCommunication::findEtudiants($idAction);
		$adc=ActionDeCommunication::findById($idAction);
		
		
		$data['fiche']=$fba;
		$data['etudiants'] = $etuParticipant;
		$data['adc'] = $adc;
		
		$this->load->view('Global/entete.php', $data);
 		$this->load->view('FicheAction/v_formulaireFBA.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creer($idAction)
	{
		$data['onglet_ouvert'] = 'actiondecommunication';
		$data['idAction']=$idAction;
		if(isset($_SESSION['utilisateur']) && $_SESSION['utilisateurRole'] == 'etudiant') // verifie que le user est bien l'etudiant
		{
			$adc=ActionDeCommunication::findById($idAction);
			$data['adc']=$adc;
			if($adc->getGroupable() == 0)
			{
				$utilisateur=unserialize($_SESSION['utilisateur']);
				
				$nDepense;
				$bDepense;
				$fDepense;
				
				$nRecette;
				$bRecette;
						
				if(!isset($_POST["NatureDepense"]))
				{
					$nDepense = null;
					$bDepense = null;
					$fDepense = null;				
				}
				else
				{
					$nDepense = $_POST["NatureDepense"];
					$bDepense = $_POST["BudgetDepense"];
					$fDepense = $_POST["Fournisseur"];
				}
				
				if(!isset($_POST["NatureRecette"]))
				{
					$nRecette = null;
					$bRecette = null;
				}
				else
				{
					$nRecette = $_POST["NatureRecette"];
					$bRecette = $_POST["BudgetRecette"];			
				}
				
	// 			if(FicheBilanAction::verifFicheBilanAction($_POST["TacheRealise"], $_POST["Echeance"], $_POST["Responsable"], $nDepense, $bDepense, $fDepense, $nRecette, $bRecette,$_POST["PointPositif"], $_POST["PointNegatif"],$_POST["BonASavoir"],$_POST["Suggestion"]))
				Tache::deltache($idAction);
				
				for($i=0;$i<count($_POST["TacheRealise"]);$i++)
				{
					$t = new Tache();
					$t->setLibelle($_POST["TacheRealise"][$i]);
					$t->setEcheance(putDateAAAAMMJJ($_POST["Echeance"][$i]));
					$t->setIdEtudiant($_POST["Responsable"][$i]);
	 				$t->setIdAction($idAction); 
	 				$t->setAuteurTache($utilisateur->getId());
					$t->addTache();
					$ta[]=$t;
				}
				
				Depense::delDepense($idAction);
				
				if($nDepense != null)
				{
					for($i=0;$i<count($nDepense);$i++)
					{
						$d = new Depense();
						$d->setLibelle($nDepense[$i]);
						$d->setBudget($bDepense[$i]);
						$d->setFournisseur($fDepense[$i]);
						$d->setIdAdc($idAction);
						$d->setAuteurDepense($utilisateur->getId());
							
						$d->addDepense();
						$dep[]=$d;
					}
				}
				
				Recette::delRecette($idAction);
							
				if($nRecette != null)
				{
					for($i=0;$i<count($_POST["NatureRecette"]);$i++)
					{
						$r = new Recette();
						$r->setLibelle($nRecette[$i]);
						$r->setBudget($bRecette[$i]);
						$r->setIdAdc($idAction);
						$r->setAuteurRecette($utilisateur->getId());
						$r->addRecette();
						
						$rec[]=$r;
					}
				
				}
			}
			
			if($fba = FicheAction::findByIdAction($idAction))
			{
				if($adc->getGroupable() != 0)
					$fba->setTravailEffectue($_POST["TravailEffectue"]);
				else
					$fba->setTravailEffectue(null);
				$fba->setPointPositif($_POST["PointPositif"]);
				$fba->setPointNegatif($_POST["PointNegatif"]);
				$fba->setBonASavoir($_POST["BonASavoir"]);
				$fba->setSuggestion($_POST["Suggestion"]);
				$fba->setIdAction($idAction);
				$fba->modifyFBA();
			}
			else 
			{
				$fba = new FicheAction();
				if($adc->getGroupable() != 0)
					$fba->setTravailEffectue($_POST["TravailEffectue"]);
				else
					$fba->setTravailEffectue(null);
				$fba->setPointPositif($_POST["PointPositif"]);
				$fba->setPointNegatif($_POST["PointNegatif"]);
				$fba->setBonASavoir($_POST["BonASavoir"]);
				$fba->setSuggestion($_POST["Suggestion"]);
				$fba->setIdAction($idAction);
				$fba->setNbEtuConfirm(0);
				$fba->setEtatFBA(0);
				$fba->addFBA();
			}
			
// 			else
// 			{
// 				$_SESSION["message"] = "Erreur champ vide";
// 				header('index.php?module=actionComm&amp;action=detail&amp;'.ID_ADC.'='.$_GET[ID_ADC]);
// 			}
// 			$adc = new ActionDeCommunication($_GET[ID_ADC]);
// 			$adc->getOneADC();
			
			if(isset($_SESSION["message"]))
			{
				echo $_SESSION["message"];
				unset($_SESSION["message"]);
			}
			
			if(!empty($rec))
				$data['recette']=$rec;
			if(!empty($dep))
				$data['depense']=$dep;
			if(!empty($ta))
				$data['tache']=$ta;
			$data['fba']=$fba;
			$data['etudiant']= ActionDeCommunication::findEtudiants($adc->getId());
			
			$this->load->view('Global/entete.php', $data);
	 		$this->load->view('FicheAction/v_confirmationFBA.php', $data);
			$this->load->view('Global/piedpage.php');		
		}
	}
	
	
	public function confirmerFiche ($idAction)
	{
		$data = array();
		$data['onglet_ouvert'] = 'actiondecommunication';
		$data['idAction']=$idAction;
		$utilisateur = unserialize($_SESSION['utilisateur']);
		
		
		$action = new ActionDeCommunication();
		$action = ActionDeCommunication::findById($idAction);
		
		$participe = new Participe();
		$participe->setAction($idAction);
		$participe->setEtudiant($utilisateur->getId());
		
		$fba = FicheAction::findByIdAction($idAction);
		$fba->setNbEtuConfirm($fba->getNbEtuConfirm()+1);
		$participe->modifyEtuConfirm();
		
		if($fba->getNbEtuConfirm() == $action->getNombreEtudiant())
			$fba->setEtatFBA(1);
		
		$fba->modifyFBA();
	
		redirect(base_url().'index.php/ActionsDeCommunication/detailAction/'.$idAction);
	}
	
	

}

?>